/*
 * Copyright (C)2016 - SMBJ Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hierynomus.mssmb2.messages

import com.hierynomus.msfscc.fileinformation.FileIdBothDirectoryInformation
import com.hierynomus.msfscc.fileinformation.FileInformationFactory
import com.hierynomus.protocol.commons.ByteArrayUtils
import com.hierynomus.smb.SMBBuffer
import spock.lang.Specification

class SMB2QueryDirectoryResponseSpec extends AbstractPacketReadSpec {

  def "should parse query directory with FileIdBothDirectoryInformation"() {
    given:
    String hexString1 = "fe534d4240000000000000000e000100010000000000000005000000000000000000000001000000010400d4005800000000000000000000000000000000000009004800320a000070000000000000002022a21b2e9fd101bffd8bc51ea0d101bffd8bc51ea0d101bffd8bc51ea0d101000000000000000000000000000000001000000002000000000000000000000000000000000000000000000000000000000000000000000075d0010000009d012e0000000000000070000000000000002022a21b2e9fd101bffd8bc51ea0d101bffd8bc51ea0d101bffd8bc51ea0d101000000000000000000000000000000001000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000002e002e0000000000700000000000000008ae559d339fd10108ae559d339fd10108ae559d339fd1012c1625a61ea0d1010000000000000000000000000000000010000000020000000000000000000000000000000000000000000000000000000000000000000000e3d4010000009f0032000000000000009000000000000000d09ce2b91ea0d101d09ce2b91ea0d101d09ce2b91ea0d101b61d2bbd1ea0d1010000000000000000000000000000000010000000240000000000000010004300520045004100540045007e00310000000000000000000000c67301000000850163007200650061007400650064005f00620079005f007300620065006c006c0061006e0000000000700000000000000024746aa4359fd10107adc13c25a0d10107adc13c25a0d10107adc13c25a0d10100000000000000000000000000000000100000000600000000000000000000000000000000000000000000000000000000000000000000001dd5010000000d004400450056000000a000000000000000ae1a43c21ea0d101ae1a43c21ea0d101ae1a43c21ea0d101bffd8bc51ea0d101000000000000000000000000000000002000000036000000000000001800460049004c0045005f0043007e0031002e005400580054000000060a020000000200660069006c0065005f0063007200650061007400650064005f00620079005f007300620065006c006c0061006e002e00740078007400000080000000000000000a1368b71da0d1010a1368b71da0d1010a1368b71da0d1012c1625a61ea0d1010000000000000000000000000000000010000000120000000000000010004e004500570046004f004c007e003100000000000000000000006cd0010000004f016e006500770066006f006c0064006500720000000000000098000000000000001eaec4e6399fd1011eaec4e6399fd1011eaec4e6399fd1012c1625a61ea0d10100000000000000000000000000000000100000002e000000000000001000570048004f004900530054007e0031000000000000000000000047d5010000001900770068006f006900730074006800690073005f0063006f006e0074006100630074005f007300610072007600610000009800000000000000164bc2e6399fd101164bc2e6399fd101164bc2e6399fd1012c1625a61ea0d10100000000000000000000000000000000100000002a000000000000000c0032004600390033007e003100000000000000000000000000000021d5010000005d00004400450056005c006a0075006e00690074005f00740065007300740073006d0062006a006100700069000000000000a0000000000000005611c7e6399fd1015611c7e6399fd1015611c7e6399fd1012c1625a61ea0d101000000000000000000000000000000001000000032000000000000000c0030003000310042007e00310000000000000000000000000000008efc010000002800004400450056005c006a0075006e00690074005f00740065007300740073006d0062006a006100700069005c0031005c0032000000000000a0000000000000009272c9e6399fd1019272c9e6399fd1019272c9e6399fd1012c1625a61ea0d101000000000000000000000000000000001000000036000000000000000c0039004600390039007e003100000000000000000000000000000097fc010000009000004400450056005c006a0075006e00690074005f00740065007300740073006d0062006a006100700069005c0031005c0032005c00330000a8000000000000003dd4cbe6399fd1013dd4cbe6399fd1013dd4cbe6399fd1012c1625a61ea0d10100000000000000000000000000000000100000003a000000000000000c0030003000430042007e0031000000000000000000000000000000b7fc01000000ff00004400450056005c006a0075006e00690074005f00740065007300740073006d0062006a006100700069005c0031005c0032005c0033005c00340000000000009800000000000000b936cee6399fd101b936cee6399fd101b936cee6399fd1012c1625a61ea0d10100000000000000000000000000000000100000002e000000000000000c0039004600440043007e0031000000000000000000000000000000bafc010000002700004400450056005c006a0075006e00690074005f00740065007300740073006d0062006a006100700069005c003200009800000000000000f998d0e6399fd101f998d0e6399fd101f998d0e6399fd1012c1625a61ea0d10100000000000000000000000000000000100000002e000000000000000c0030003000440033007e0031000000000000000000000000000000c1fc010000007500004400450056005c006a0075006e00690074005f00740065007300740073006d0062006a006100700069005c003300009800000000000000a45dd5e6399fd101a45dd5e6399fd101a45dd5e6399fd1012c1625a61ea0d10100000000000000000000000000000000100000002e000000000000000c0030003000450031007e0031000000000000000000000000000000c2fc010000005e00004400450056005c006a0075006e00690074005f00740065007300740073006d0062006a006100700069005c00340000a000000000000000fdbfd7e6399fd101fdbfd7e6399fd101fdbfd7e6399fd1012c1625a61ea0d101000000000000000000000000000000001000000032000000000000000c0030003000380043007e0031000000000000000000000000000000d8fc010000001d00004400450056005c006a0075006e00690074005f00740065007300740073006d0062006a006100700069005c0034005c0032000000000000a0000000000000000223dae6399fd1010223dae6399fd1010223dae6399fd1012c1625a61ea0d101000000000000000000000000000000001000000036000000000000000c0039004600320044007e0031000000000000000000000000000000d9fc010000004400004400450056005c006a0075006e00690074005f00740065007300740073006d0062006a006100700069005c0034005c0032005c0033000000000000000000000f85dce6399fd1010f85dce6399fd1010f85dce6399fd1012c1625a61ea0d10100000000000000000000000000000000100000003a000000000000000c0039004600300032007e0031000000000000000000000000000000dffc010000000501004400450056005c006a0075006e00690074005f00740065007300740073006d0062006a006100700069005c0034005c0032005c0033005c0034"
    byte[] bytes1 = ByteArrayUtils.parseHex(hexString1)

    when:
    def response = convert(bytes1)

    then:
    response.class == SMB2QueryDirectoryResponse.class

    when:
    def fileInfoList = FileInformationFactory.parseFileInformationList((response as SMB2QueryDirectoryResponse).outputBuffer, FileInformationFactory.getDecoder(FileIdBothDirectoryInformation.class))

    then:
    fileInfoList.size() == 18
    fileInfoList.get(0).fileName == "."
    fileInfoList.get(1).fileName == ".."
    fileInfoList.get(2).fileName == "2"
    fileInfoList.get(8).fileName == "䐀䔀嘀尀樀甀渀椀琀开琀攀猀琀猀洀戀樀愀瀀椀"
  }
}
